package loginbean;

import java.io.IOException;
import java.util.Vector;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class WorkFlowController extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	public void init ( ServletConfig conf ) throws ServletException
	{
		super.init(conf);
	}
	public void doPost ( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException
	{
		HttpSession session = req.getSession( true );
		int tenantId = Integer.parseInt(session.getAttribute("tenantid").toString());
		
		String url = null;
		String viewcmd = req.getParameter( "viewcmd" );
		Vector<Integer> webServiceOrder = null;
		Vector<String> webServiceNames = null;
		switch( viewcmd.charAt( 0 ) )
		{
			case 'A':
			{
				String newWFName = req.getParameter( "workflowname" );
				if(newWFName == null)
				{
					url = "/CreateWorkFlow.jsp";
				}
				else
				{
					int index = 1;
					String param = "services", test = param + index;
					Vector<String> webSvcs = new Vector<String>();
					do
					{
						webSvcs.addElement(req.getParameter(test));
						index ++;
						test = param + index;
					}
					while(req.getParameter(test) != null);
					WorkFlow.insertNewWorkFlow(tenantId, newWFName, webSvcs);
					url = "/welcome.jsp";
				}

				break;
			}
			case 'B':
			{
				webServiceNames = WorkFlow.retrieveFlowServices(tenantId, req.getParameter( "flows" ));
				webServiceOrder = WorkFlow.retrieveFlowOrder(tenantId, req.getParameter( "flows" ));
				session.setAttribute("workFlowName", req.getParameter( "flows" ));
				session.setAttribute("serviceNames", webServiceNames);
				session.setAttribute("serviceOrder", webServiceOrder);
				url = "/CustomizeWorkFlow.jsp";
				break;
			}
			case 'C':
			{
				int index = 2;
				String param = "test", test = param + index;
				Vector<String[]> newOrder = new Vector<String[]>();
				do
				{
					System.out.println(req.getParameter(test));
					String[] inputs = req.getParameter(test).split("-");
					newOrder.addElement(inputs);
					index ++;
					test = param + index;
				}
				while(req.getParameter(test) != null);
				String workFlowName = (String) session.getAttribute( "workFlowName" );
				WorkFlow.changeSvcOrder(tenantId, workFlowName, newOrder);
				url = "/welcome.jsp";
				break;
			}
			case 'D'://delete a webservice from the current workflow
			{
				int svcOrderNumber = new Integer(req.getParameter( "del" )).intValue();
				String workFlowName = (String) session.getAttribute( "workFlowName" );
				WorkFlow.deleteService(tenantId, workFlowName, svcOrderNumber);
				url = "/CustomizeWorkFlow.jsp";
				break;
			}
			case 'E'://add a new webservice to the current workflow then retrieve the workflow so it can be printed again.
			{
				String newServiceName = req.getParameter( "services" );
				String workFlowName = (String) session.getAttribute( "workFlowName" );
				//System.out.println(workFlowName);
				WorkFlow.insertNewService(tenantId, workFlowName, newServiceName);
				url = "/CustomizeWorkFlow.jsp";
				break;
			}
		}
		session.setAttribute("serviceNames", webServiceNames);
		session.setAttribute("serviceOrder", webServiceOrder);
		ServletContext sc = getServletContext();
		RequestDispatcher rd = sc.getRequestDispatcher(url);
		rd.forward(req, res);
	}
}
